﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AmiEmuLib.AmigaConfiguration;
using System.Xml;
using AmiEmuLib.Configuration.AmigaEventsConfig;
using TomanuExtensions;

namespace AmiEmuLib.Configuration.KeyboardConfig
{
    /// <summary>
    /// Maps amiga events to raw codes. For specific amigsa model.
    /// </summary>
    public class RawCodes
    {
        /// <summary>
        /// One or more amiga models.
        /// </summary>
        public readonly AmigaModel AmigaModel;

        /// <summary>
        /// List of raw codes.
        /// </summary>
        private List<RawCode> m_raw_codes = new List<RawCode>();

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="a_reader"></param>
        internal RawCodes(XmlReader a_reader)
        {
            AmigaModel = a_reader.GetAttributeEnum<AmigaModel>("AmigaModel");

            if (a_reader.IsEmptyElement)
            {
                a_reader.ReadStartElement();
                return;
            }

            a_reader.ReadStartElement(GetType().Name);

            while (a_reader.IsStartElement())
                m_raw_codes.Add(new RawCode(a_reader));

            a_reader.ReadEndElement();
        }

        /// <summary>
        /// List of raw codes.
        /// </summary>
        public IEnumerable<RawCode> List
        {
            get
            {
                return m_raw_codes;
            }
        }

        /// <summary>
        /// Write xml.
        /// </summary>
        /// <param name="a_writer"></param>
        internal void WriteXml(XmlWriter a_writer)
        {
            a_writer.WriteStartElement(GetType().Name);

            a_writer.WriteAttributeEnum<AmigaModel>("AmigaModel", AmigaModel);

            foreach (var raw_code in m_raw_codes)
                raw_code.WriteXml(a_writer);

            a_writer.WriteEndElement();
        }
    }
}
